package com.agile.leetcode.hot.easy;

import java.util.HashSet;
import java.util.Set;

/**
 * 定一个字符串s ，请你找出其中不含有重复字符的最长连续子字符串的长度。
 *
 * @Author:ChenZhangKun
 * @Date: 2021/10/12 23:15
 */
public class lengthOfLongestSubstring2 {
    public static void main(String[] args) {
        String str = "czjkk1994";
        System.out.println(findMaxLength(str));
    }

    public static int findMaxLength(String str) {
        // 左右指针
        int left = 0;
        int right = 0;
        // 定义变量接收长度
        int maxLen = 1;
        Set<Character> set = new HashSet<>();
        while (right < str.length()) {
            Character rightChar = str.charAt(right);
            while (set.contains(rightChar)) {
                // 移除
                set.remove(str.charAt(left));
                left++;
            }
            // 加入右边
            set.add(rightChar);
            maxLen = Math.max(maxLen, right - left + 1);
            right++;
        }
        return maxLen;
    }
}
